//
//  PARealNameAuthViewController.m
//  PAMobileWallet
//
//  Created by Pay_SMP002 on 13-7-3.
//  Copyright (c) 2013年 平安付. All rights reserved.
//

#import "PARealNameAuthViewController.h"
#import "PARealNameAuthView.h"
#import "Global.h"
#import "PAResultViewController.h"
#import "PANavView.h"
#import "PAMobileRegisterViewController.h"
#import "PAReadagreementViewController.h"
#import "PAPersonalViewController.h"
#import "PADataCenter.h"
#import "PANetworkManager.h"
#import "PAChekText.h"

@interface PARealNameAuthViewController ()

@end

@implementation PARealNameAuthViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        authScrollerView = [[UIScrollView alloc] initWithFrame:RECT_PHONE];
        self.view = authScrollerView;
        authScrollerView.delegate = self;
        authScrollerView.pagingEnabled = NO;
        authScrollerView.showsVerticalScrollIndicator = NO;
        authScrollerView.showsHorizontalScrollIndicator = NO;
        authScrollerView.backgroundColor = [UIColor clearColor];
        CGSize newSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height + 230);
        [authScrollerView setContentSize:newSize];
        self.view.backgroundColor = BGVIEW_COLOR;
        //[self.view addSubview:authScrollerView];
        
        realAuthView = [[PARealNameAuthView alloc] initWithFrame:CGRectMake(0, 0, WIDTH_PHONE, HEIGHT_CURRENT - 20)];
        realAuthView.parentController = self;
        realAuthView.nameTF.delegate = self;
        realAuthView.cardTF.delegate = self;
        realAuthView.loginpassTF.delegate = self;
        realAuthView.paypassTF.delegate = self;
        [authScrollerView addSubview:realAuthView];
        
        navView = [[PANavView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
        navView.delegate = self;
        [navView setTitle:@"实名认证"];
        [self.view addSubview:navView];
    }
    return self;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == realAuthView.nameTF)
    {
		[realAuthView.cardTF becomeFirstResponder];
	}
	else if (textField == realAuthView.cardTF)
    {
		[realAuthView.loginpassTF becomeFirstResponder];
	}
    else if (textField == realAuthView.loginpassTF)
    {
        [realAuthView.paypassTF becomeFirstResponder];
    }
    else if (textField == realAuthView.paypassTF)
    {
        [realAuthView.paypassTF resignFirstResponder];
        [authScrollerView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
    
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == realAuthView.nameTF)
    {
        realAuthView.nameImage.image = [UIImage imageNamed:@"textbox2.png"];
        realAuthView.cardImage.image = [UIImage imageNamed:@"card_enter.png"];
        realAuthView.loginpassImage.image = [UIImage imageNamed:@"card_enter.png"];
        realAuthView.paypassImage.image = [UIImage imageNamed:@"card_enter.png"];
	}
	else if (textField == realAuthView.cardTF)
    {
        realAuthView.nameImage.image = [UIImage imageNamed:@"card_enter.png"];
        realAuthView.cardImage.image = [UIImage imageNamed:@"textbox2.png"];
        realAuthView.loginpassImage.image = [UIImage imageNamed:@"card_enter.png"];
        realAuthView.paypassImage.image = [UIImage imageNamed:@"card_enter.png"];
	}
    else if (textField == realAuthView.loginpassTF)
    {
        realAuthView.nameImage.image = [UIImage imageNamed:@"card_enter.png"];
        realAuthView.cardImage.image = [UIImage imageNamed:@"card_enter.png"];
        realAuthView.loginpassImage.image = [UIImage imageNamed:@"textbox2.png"];
        realAuthView.paypassImage.image = [UIImage imageNamed:@"card_enter.png"];
    }
    else if (textField == realAuthView.paypassTF)
    {
        realAuthView.nameImage.image = [UIImage imageNamed:@"card_enter.png"];
        realAuthView.cardImage.image = [UIImage imageNamed:@"card_enter.png"];
        realAuthView.loginpassImage.image = [UIImage imageNamed:@"card_enter.png"];
        realAuthView.paypassImage.image = [UIImage imageNamed:@"textbox2.png"];
        
        [authScrollerView setContentOffset:CGPointMake(0, 100) animated:YES];
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    //
}

- (void)changeAllFram:(CGRect)getRect
{

}

#pragma mark - PANavViewDelegate methods

- (void)sendSelf:(PANavView *)sendView
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)sendButton:(id)sender
{
    UIButton * receiveButton = (UIButton *)sender;
    
    NSUserDefaults * mobileRegisterUserName = [NSUserDefaults standardUserDefaults];
    NSString * userNameString = (NSString *)[mobileRegisterUserName objectForKey:@"mobileRegisterUserName"];

    if (receiveButton.tag == 10)  // 阅读协议
    {
        if (!readagreementViewController)
        {
            readagreementViewController = [[PAReadagreementViewController alloc] init];
            readagreementViewController.parentController = self;
        }
        
        [readagreementViewController sendShowType:1];
        [self.navigationController presentViewController:readagreementViewController animated:YES completion:nil];
    }
    else if (receiveButton.tag == 20)  // 点击注册
    {
        if ([realAuthView.nameTF.text length]>0 && [realAuthView.cardTF.text length]>0 && [PAChekText validCardID:realAuthView.cardTF.text]==1 && [realAuthView.loginpassTF.text length]>0 && [PAChekText validSetLoginPW:realAuthView.loginpassTF.text]==1 && [realAuthView.paypassTF.text length]>0 && [PAChekText validSetPayPW:realAuthView.paypassTF.text]==1 && [userNameString length]>0 )
        {
            NSMutableDictionary *postingDic = [[NSMutableDictionary alloc] initWithDictionary:@{k_operationType:@"op_register_real_name",k_loginId:userNameString,k_realName:realAuthView.nameTF.text,k_idCardType:[NSNumber numberWithInt:1],k_idCardNo:realAuthView.cardTF.text,k_loginPassword:realAuthView.loginpassTF.text,k_payPassword:realAuthView.paypassTF.text}];
            [[PANetworkManager sharedInstance] reqAsynParamDic:postingDic userInfo:nil delegate:self];
        }
        else
        {
            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"以上信息填写有误，请重新填写" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
    }
}

- (void)paRequestRespons:(NSDictionary *)responsDic userInfo:(NSDictionary *)userInfo
{
    NSLog(@"responDic:%@",responsDic);
    
    NSString *op = [responsDic objectForKey:k_operationType];
    if ([op isEqualToString:@"op_register_real_name"])
    {
        if (!resultController)
        {
            resultController = [[PAResultViewController alloc] initWithNibName:nil bundle:nil];
            resultController.parentController = self;
        }
        [resultController setResultType:RESULT_TYPE_register];
        [resultController setTitleName:@"注册结果"];
        [self.navigationController pushViewController:resultController animated:YES];
        
        NSUserDefaults * mobileRegisterUserName = [NSUserDefaults standardUserDefaults];
        NSString * userNameString = (NSString *)[mobileRegisterUserName objectForKey:@"mobileRegisterUserName"];
        
        PAPersonalViewController *personVC = [self findPersonCenterViewConteroller];
        [personVC loginAction:[NSDictionary dictionaryWithObjectsAndKeys:userNameString, k_loginId,realAuthView.loginpassTF.text,k_loginPassword,nil]];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    [realAuthView.nameTF addTarget:self action:@selector(checknameTF:) forControlEvents:UIControlEventEditingChanged];
    [realAuthView.cardTF addTarget:self action:@selector(checkCardTFState:) forControlEvents:UIControlEventEditingChanged];
    [realAuthView.loginpassTF addTarget:self action:@selector(checkLoginpassTF:) forControlEvents:UIControlEventEditingChanged];
    [realAuthView.paypassTF addTarget:self action:@selector(checkPaypassTF:) forControlEvents:UIControlEventEditingChanged];
}

- (void)checknameTF:(UITextField *)getTF
{
    /*
    if ([getTF.text length]>0 && [PAChekText validUserName:getTF.text]<0) {
        [PAChekText tipingAlert:@"名字格式不正确，请重新输入" isCancle:@""];
    }
     */
    [self changeConfimButton];
}

- (void)checkLoginpassTF:(UITextField *)getTF
{
    /*
    if ([getTF.text length]>0 && [PAChekText validSetLoginPW:getTF.text]<0) {
        [PAChekText tipingAlert:@"登录密码格式不正确，请重新输入" isCancle:@""];
    }
    */
    [self changeConfimButton];
}

// 身份证验证（是否合法）
- (void)checkCardTFState:(UITextField *)getTF
{
    /*
    if ([getTF.text length]>0 && [PAChekText validCertiNo:getTF.text]<0) {
        [PAChekText tipingAlert:@"身份证格式不正确，请重新输入" isCancle:@""];
    }
    */
    [self changeConfimButton];
}

- (void)checkPaypassTF:(UITextField *)getTF
{
    /*
    if ([getTF.text length]>0 && [PAChekText validCertiNo:getTF.text]<0) {
        [PAChekText tipingAlert:@"支付密码格式不正确，请重新输入" isCancle:@""];
    }
     */
    
    if ([realAuthView.loginpassTF.text isEqualToString:realAuthView.paypassTF.text]) {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"登录密码和支付密码不能相同" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
    [self changeConfimButton];
}

- (void)changeConfimButton
{
    if ([realAuthView.nameTF.text length]>0 && [realAuthView.cardTF.text length]>0 && [realAuthView.loginpassTF.text length]>0 && [realAuthView.paypassTF.text length] && [realAuthView.paypassTF.text length]>0 && realAuthView.agreeButton.selected == NO)
    {
        [realAuthView.authButton setBackgroundImage:[UIImage imageNamed:@"button_A1.png"] forState:UIControlStateNormal];
        [realAuthView.authButton setBackgroundImage:[UIImage imageNamed:@"button_A2.png"] forState:UIControlStateHighlighted];
        [realAuthView.authButton setTitleColor:ABLE_COLOR forState:UIControlStateNormal];
        [realAuthView.authButton setTitleColor:ABLE_COLOR forState:UIControlStateHighlighted];
        realAuthView.authButton.enabled = YES;
    }
    else
    {
        [realAuthView.authButton setBackgroundImage:[UIImage imageNamed:@"button_B1.png"] forState:UIControlStateNormal];
        realAuthView.authButton.enabled = NO;
        realAuthView.authButton.titleLabel.textColor = ENABLE_COLOR;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
